package com.example.busshop.service;

import com.example.busshop.common.service.LogicService;
import com.example.busshop.common.utils.TokenUtil;
import com.example.busshop.config.R;
import com.example.busshop.dao.UserDao;
import com.example.busshop.entity.User;
import org.modelmapper.internal.util.CopyOnWriteLinkedHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@Service
public class UserService extends LogicService<UserDao, User,Long> {
    @Resource
    TokenUtil tokenUtil;
    public UserService(@Autowired UserDao dao) {
        super(dao);
    }
    public Object loginCheck(User user, HttpServletResponse response) {
        User user2 = dao.selectByName(user.getUsername());
        System.out.println(user2.getPassword());
        System.out.println(user.getPassword());
        if (user2 == null) {
            return R.error().message("该用户不存在！");
        }
        if (!user2.getPassword().equals(user.getPassword())) {
            return R.error().message("密码错误！");
        }
        String token = tokenUtil.generateToken(user2);
        System.out.println(token);
        Cookie cookie = new Cookie("token", token);
//        设置cookie的作用域：为”/“时，以在webapp文件夹下的所有应用共享cookie
        cookie.setPath("/");
        response.addCookie(cookie);

        return user2;
    }
}
